'use strict';
const Controller = require('egg').Controller;

class UserController extends Controller {
  //注册
  async regist() {
    let { tel, psd } = this.ctx.request.body;
    let { code, msg } = await this.service.user.regist(tel, psd);
    this.ctx.body = this.service.utility.responseBody(code, msg);
  }
  //登录
  async login() {
    let { type, tel, psd, captcha } = this.ctx.request.body;
    let end = await this.service.user.login(type, tel, psd, captcha);
    this.ctx.body = end;
  }
  //退出
  async out() {
    let { id } = this.ctx.query;
    console.log(id)
    let end = await this.service.user.out(id);
    this.ctx.body = end;
  }
  //优惠券
  async getVoucher() {
    let { userId } = this.ctx.query;
    let end = await this.service.user.getVoucher(userId);
    this.ctx.body = end;
  }
  //修改头像
  async updateAvatar() {

  }
  //修改资料
  async updateInfo() {
    let code = 1, msg = "修改成功！";
    let { userId, nickname, psd, name, sex, birthday } = this.ctx.query;
    let end = await this.service.user.updateInfo(userId, nickname, psd, name, sex, birthday);
    if (end.affectedRows.length === 0) {
      code = 0;
      msg = "修改失败！";
    }
    this.ctx.body = this.service.utility.responseBody(code, msg);
  }
}

module.exports = UserController;
